<template>
  <div class="container">
    <!--  -->
    <van-nav-bar
      fixed
      left-arrow
      @click-left="$router.back()"
      title="小智同学"
    ></van-nav-bar>
    <div class="chat-list">
      <!-- 左侧是机器人小智 -->
      <div v-for="(item, index) in zhilist" :key="index">
        <div class="chat-item left" v-if="item.type === 1">
          <van-image
            fit="cover"
            round
            :src="require('./adc904f83e05601efa57f61941e3f2a.png')"
          />
          <div class="chat-pao">{{ item.msg }}</div>
        </div>

        <!-- 右侧是当前用户 -->
        <div class="chat-item right" v-else>
          <div class="chat-pao my">{{ item.msg }}</div>
          <van-image fit="cover" round :src="list.photo" />
        </div>
      </div>
    </div>
    <div class="reply-container van-hairline--top">
      <van-field v-model.trim="word" placeholder="说点什么...">
        <span
          @click="send()"
          slot="button"
          @keyup.enter="send()"
          style="font-size: 12px; color: #999"
        >
          提交
        </span>
      </van-field>
    </div>
  </div>
</template>

<script>
import io from 'socket.io-client'
import { getUser } from '@/api/user'
import { mapState } from 'vuex'
export default {
  name: 'UserChat',
  data() {
    return {
      word: '',
      list: [],
      zhilist: [
        // type: 1 小智的消息
        // type: 2 用户发的消息

        {
          type: 1,
          msg: '你好，我是小智',
          timestamp: Date.now()
        },
        {
          type: 2,
          msg: '我是编程小王子！',
          timestamp: Date.now()
        },
        {
          type: 1,
          msg: '你以为会在百度上抄代码，就是程序员了吗？',
          timestamp: Date.now()
        },
        {
          type: 2,
          msg: '不是这样吗？',
          timestamp: Date.now()
        }
      ]
    }
  },
  computed: {
    ...mapState('user', ['token'])
  },
  async created() {
    const { data: res } = await getUser()
    // console.log(res)
    this.list = res
    const socket = io('http://toutiao.itheima.net', {
      query: {
        token: this.token.token
      },
      transports: ['websocket']
    })
    // console.log(socket)
    this.socket = socket
    socket.on('connect', () => {
      // console.log('123')
      this.$toast('服务器连接成功')
    })
    socket.on('message', (data) => {
      // console.log(data)
      this.zhilist.push({
        type: 1,
        msg: data.msg,
        timestamp: Date.now()
      })
    })
  },

  methods: {
    send() {
      const info = {
        msg: this.word,
        timestamp: Date.now()
      }
      this.socket.emit('message', info)
      info.type = 2
      this.zhilist.push(info)
      // console.log(this.word)
      this.word = ''
    }
  },
  watch: {},
  beforeDestroy() {
    this.socket.close()
  }
}
</script>

<style lang="less" scoped>
.container {
  height: 100%;
  width: 100%;
  position: absolute;
  left: 0;
  top: 0;
  box-sizing: border-box;
  background: #fafafa;
  padding: 46px 0 50px 0;
  .chat-list {
    height: 100%;
    overflow-y: scroll;
    .chat-item {
      padding: 10px;
      .van-image {
        vertical-align: top;
        width: 40px;
        height: 40px;
      }
      .chat-pao {
        vertical-align: top;
        display: inline-block;
        min-width: 40px;
        max-width: 70%;
        min-height: 40px;
        line-height: 38px;
        border: 0.5px solid #c2d9ea;
        border-radius: 4px;
        position: relative;
        padding: 0 10px;
        background-color: #e0effb;
        word-break: break-all;
        font-size: 14px;
        color: #333;
        &::before {
          content: '';
          width: 10px;
          height: 10px;
          position: absolute;
          top: 12px;
          border-top: 0.5px solid #c2d9ea;
          border-right: 0.5px solid #c2d9ea;
          background: #e0effb;
        }
      }
      .chat-pao.my {
        background-color: #9eea6a;
        &::before {
          content: '';
          background: #9eea6a;
        }
      }
    }
  }
}
.chat-item.right {
  text-align: right;
  .chat-pao {
    margin-left: 0;
    margin-right: 15px;
    &::before {
      right: -6px;
      transform: rotate(45deg);
    }
  }
}
.chat-item.left {
  text-align: left;
  .chat-pao {
    margin-left: 15px;
    margin-right: 0;
    &::before {
      left: -5px;
      transform: rotate(-135deg);
    }
  }
}
.reply-container {
  position: fixed;
  left: 0;
  bottom: 0;
  height: 44px;
  width: 100%;
  background: #f5f5f5;
  z-index: 9999;
}
</style>
